package com.apartmentManage.service.impl;

import com.apartmentManage.dao.IntoPersonnelDao;
import com.apartmentManage.dao.RoomDao;
import com.apartmentManage.entity.IntoPersonnel;
import com.apartmentManage.service.IntoPersonnelService;
import com.apartmentManage.util.StringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IntoPersonnelServiceImpl implements IntoPersonnelService {

    @Autowired
    private IntoPersonnelDao intoPersonnelDao;

    @Autowired
    private RoomDao roomDao;

    @Override
    public boolean save(IntoPersonnel intoPersonnel) {
        if (StringUntil.IsBlank(intoPersonnel.getSno())
                ||StringUntil.IsBlank(intoPersonnel.getSname())
        || StringUntil.IsBlank(intoPersonnel.getSsex())
                || StringUntil.IsBlank(intoPersonnel.getTelephone())
        )
            return false;
        List<String> allNo = getAllNo();
        for (String no :allNo){
            if (no.equals(intoPersonnel.getSno()))
                return false;
        }
        if (intoPersonnelDao.save(intoPersonnel)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean saveRoom(Integer roomno, String sno) {
        if (roomno==null || roomno==0)
            return false;
        roomDao.reduceRoom(roomno);
        IntoPersonnel intoPersonnel=new IntoPersonnel();
        intoPersonnel.setRoomno(roomno);
        intoPersonnel.setSno(sno);
        return intoPersonnelDao.saveRoom(intoPersonnel)>0;
    }

    @Override
    public boolean update(IntoPersonnel intoPersonnel) {
        if (StringUntil.IsBlank(intoPersonnel.getSname())
                || StringUntil.IsBlank(intoPersonnel.getSsex())
                || StringUntil.IsBlank(intoPersonnel.getTelephone())
                ||StringUntil.IsBlank(intoPersonnel.getApartmentno())
                ||StringUntil.IsBlank(intoPersonnel.getRoomno()))
            return false;
        /**
         * 判断是否更新房间号
         * 如果相等就直接执行
         * 不相等老的房间号增加，新房间号增加
         */
        if (getByNo(intoPersonnel.getSno()).getRoomno()==intoPersonnel.getRoomno()){
            return intoPersonnelDao.update(intoPersonnel)>0;
        }else {
//            房间数减少
            roomDao.reduceRoom(intoPersonnel.getRoomno());
//            房间数增加
            roomDao.addRoom(getByNo(intoPersonnel.getSno()).getRoomno());
            return intoPersonnelDao.update(intoPersonnel)>0;
        }
    }

    @Override
    public boolean delete(String sno) {
        Integer roomno = intoPersonnelDao.getByNo(sno).getRoomno();
        if (intoPersonnelDao.delete(sno)>0){
            if (roomno!=null && roomno!=0)
                   roomDao.addRoom(roomno);
            return true;
        }
        return false;
    }

    @Override
    public List<IntoPersonnel> getAll() {
        return intoPersonnelDao.getAll();
    }

    @Override
    public IntoPersonnel getByNo(String sno) {
        return intoPersonnelDao.getByNo(sno);
    }

    @Override
    public List<IntoPersonnel> getByRequirement(IntoPersonnel intoPersonnel) {
        return intoPersonnelDao.getByRequirement(intoPersonnel);
    }

    @Override
    public List<String> getAllNo() {
        return intoPersonnelDao.getAllNo();
    }
}
